package com.weiweiqin.controller.home;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.weiweiqin.constants.Constants;
import com.weiweiqin.model.Color;
import com.weiweiqin.service.BrandsService;
import com.weiweiqin.service.ColorService;
import com.weiweiqin.service.GoodsService;
import com.weiweiqin.service.PicService;
import com.weiweiqin.utils.CompressPicUtil;

/**
 * 
 * @author weiweiqin
 * 
 */
@Controller
@RequestMapping("front/upload")
public class FUploadController {
	
	/**
	 * 图片压缩质量
	 */
	private static final float QUALITY = 0.97f;

	@Autowired
	private GoodsService goodsService;
	
	@Autowired
	private PicService picService;
	
	@Autowired
	private ColorService colorService;
	
	@Autowired
	private BrandsService brandsService;
	
	@RequestMapping("download.do")
	public void downloadFile(String fileName, HttpServletRequest request,
			HttpServletResponse response) {
		response.setCharacterEncoding("utf-8");
		response.setContentType("multipart/form-data");

		response.setHeader("Content-Disposition", "attachment;fileName="
				+ fileName);
		try {
			String webapps = getTomcatWebappsPath(request);
			String filePath = webapps + File.separator
					+ Constants.FileInfo.FILE_PATH;
			File file = new File(fileName);
			InputStream inputStream = new FileInputStream(filePath + "/" + file);
			OutputStream os = response.getOutputStream();
			byte[] b = new byte[1024];
			int length;
			while ((length = inputStream.read(b)) > 0) {
				os.write(b, 0, length);
			}
			inputStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@RequestMapping("downloadCompress.do")
	public void downloadCompress(String fileName, HttpServletRequest request,
			HttpServletResponse response) {
		response.setCharacterEncoding("utf-8");
		response.setContentType("multipart/form-data");

		response.setHeader("Content-Disposition", "attachment;fileName="
				+ fileName);
		try {
			String webapps = getTomcatWebappsPath(request);
			String filePath = webapps + File.separator
					+ Constants.FileInfo.COMPRESS_FILE_PATH;
			File file = new File(fileName);
			InputStream inputStream = new FileInputStream(filePath + "/" + file);
			OutputStream os = response.getOutputStream();
			byte[] b = new byte[1024];
			int length;
			while ((length = inputStream.read(b)) > 0) {
				os.write(b, 0, length);
			}
			inputStream.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 获取tomcat webapps目录
	 * 
	 * @param request
	 * @return
	 */
	private static String getTomcatWebappsPath(HttpServletRequest request) {
		String result = "";
		String tomcatRoot = request.getSession().getServletContext()
				.getRealPath("/");
		int indexOf = tomcatRoot.indexOf("webapps");
		if (indexOf != -1) {
			result = tomcatRoot.substring(0, indexOf + 7);
		}else {
			result = "/Users/wwq/projects/";
		}
		return result;
	}
	
	//改变历史颜色图片的压缩图片大小
	@RequestMapping("setCompressColor.do")
	public void setCompressColor(HttpServletRequest request,
			HttpServletResponse response){
		
		String webapps = getTomcatWebappsPath(request);
		// 原始图片存放路径
		String filePath = webapps + File.separator
						+ Constants.FileInfo.FILE_PATH;
		
		// 压缩图片存放路径
		String compressFilePath = webapps + File.separator
						+ "colorPic";
		int number = 0;
	
		//删除原始图片
		File folder = new File(filePath);
		File[] files = folder.listFiles();
		for (int i=0;i<files.length;i++){
		  File file = files[i];
		  String picName = file.getName();
		  List<Color> colorList = colorService.getByPicName(picName);
		  if(null != colorList && !colorList.isEmpty()){
			// 压缩图片
				CompressPicUtil
						.createThumbnail(filePath + "/" + picName,
								compressFilePath + "/" + picName, QUALITY, 0,
								50);
			  number++;
			  
		  }
		}
		System.out.println("number="+number);
	}
}